<?php
include_once 'protoweb_controller.php';

/**--------------------------------------------------------------------------
 * Please, don't touch this file.
 *
 * If you need to custom this Class, you can edit users.php
 * file overwrite and define new methods
 *---------------------------------------------------------------------------
 */

/**
 * @package Protoweb
 * @subpackage controller
 */
class Users_protoweb extends Protoweb_controller {

  public function __construct() {
    parent::__construct();
  } // end of method __construct()



  public function edit($id=0) {
    $data["user"] = new User($id);

    if ($this->input->post("posting") == 1)
      $data = $this->edit_save($data["user"]->id);

    // To one relationships
    
    // To many relationships
    
    $permissions = new Permission();
    $data["permissions"] = $permissions->all_to_asoc_array("id","access");
    $data["permissions_selected"] = $data["user"]->get_relationship_ids("permission");
    
    $this->smarty_parser->view('user_edit.html', $data);
  } // end of method edit($id=0)


  private function edit_save($id) {
    $data["error"] = "";
    $data["user"] = new User($id);

          // Assign value to field. If value is empty, we will assign NULL
      $data["user"]->username = $this->input->post("username");
      if ($data["user"]->username == "") $data["user"]->username = NULL;
      
      // files for upload
      /*
      if ($data["user"]->username->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(username)) $data["user"]->username = $fileupload.clean_file_name(username);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["user"]->password = $this->input->post("password");
      if ($data["user"]->password == "") $data["user"]->password = NULL;
      
      // files for upload
      /*
      if ($data["user"]->password->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(password)) $data["user"]->password = $fileupload.clean_file_name(password);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["user"]->lastname = $this->input->post("lastname");
      if ($data["user"]->lastname == "") $data["user"]->lastname = NULL;
      
      // files for upload
      /*
      if ($data["user"]->lastname->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(lastname)) $data["user"]->lastname = $fileupload.clean_file_name(lastname);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["user"]->firstname = $this->input->post("firstname");
      if ($data["user"]->firstname == "") $data["user"]->firstname = NULL;
      
      // files for upload
      /*
      if ($data["user"]->firstname->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(firstname)) $data["user"]->firstname = $fileupload.clean_file_name(firstname);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["user"]->email = $this->input->post("email");
      if ($data["user"]->email == "") $data["user"]->email = NULL;
      
      // files for upload
      /*
      if ($data["user"]->email->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(email)) $data["user"]->email = $fileupload.clean_file_name(email);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["user"]->phone = $this->input->post("phone");
      if ($data["user"]->phone == "") $data["user"]->phone = NULL;
      
      // files for upload
      /*
      if ($data["user"]->phone->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(phone)) $data["user"]->phone = $fileupload.clean_file_name(phone);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["user"]->validationdate = $this->input->post("validationdate");
      if ($data["user"]->validationdate == "") $data["user"]->validationdate = NULL;
      
      // files for upload
      /*
      if ($data["user"]->validationdate->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(validationdate)) $data["user"]->validationdate = $fileupload.clean_file_name(validationdate);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["user"]->activationcode = $this->input->post("activationcode");
      if ($data["user"]->activationcode == "") $data["user"]->activationcode = NULL;
      
      // files for upload
      /*
      if ($data["user"]->activationcode->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(activationcode)) $data["user"]->activationcode = $fileupload.clean_file_name(activationcode);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
    
    //saving attributes
    if(! $data["user"]->save())
      $data["error"] .= $data["user"]->error->string;

    //saving to one relationships
    
    //to many relationships
            
    redirect(base_url()."users/edit/".$data["user"]->id);
  } // end of method edit_save($id)


  public function delete($id) {
    $data["error"] = "";
    $data["user"] = new User($id);

    //delete
    if(! $data["user"]->delete())
      $data["error"] .= $data["user"]->error->string;

    redirect(base_url()."users/edit/");
  } // end of method delete($id)

    
} // class Users_protoweb

/* End of file users_protoweb.php */
/* Location: ./system/application/controllers/users_protoweb.php */
